Repository navigation
fix: accept tokens containing hyphens in the Authorization header - #1221
Chandra-Prakash-TS wants to merge 1 commit into
Conversation
Tokens are created as `${config.cnpmcore.name}_<random>`, so a registry
whose name contains a hyphen (e.g. `local-demo`) issues tokens that the
Bearer token pattern `[\w.]+` never matches, and every request with such
a token fails with "Invalid token". Allow `-` in the token pattern.
Fixes cnpm#1144
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
Fixes #1144
Problem
Tokens are created by
randomToken(this.config.cnpmcore.name)as<name>_<random>_<crc>. When the registrynamecontains a hyphen (for examplelocal-demo), the token contains one too.TokenService.getUserAndToken()only accepts^Bearer ([\w.]+?)$, so such a token is rejected before it is looked up, and every authenticated request (for exampleGET /-/whoami) fails with401 Invalid token.Fix
Allow
-inBEARER_TOKEN_RE. Tokens are still looked up by their sha512 hash, so an unknown token is still rejected withInvalid token(the existingBearer foo-tokenassertion inwhoami.test.tsstill passes).Tests
Added a case to
test/port/controller/UserController/whoami.test.tsthat setsconfig.cnpmcore.nametolocal-demo, creates a user and calls/-/whoamiwith the returned token.expected 200 "OK", got 401 "Unauthorized"Also ran
test/port/middleware/AlwaysAuth.test.tsandtest/port/controller/TokenController/createToken.test.ts(MySQL), plusvp lint,vp fmt --checkandtsc --noEmit.Summary by CodeRabbit